package org.example.service.impl;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.config.ApiConfig;
import org.example.entity.Auth;
import org.example.service.AuthService;
import org.example.util.constantUtil.ConstantUtil;
import org.example.util.HttpClientUtil;
import org.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static org.example.enums.ResultCode.*;
@Service
@Slf4j
public class AuthServiceImpl implements AuthService {

    @Autowired
    private ApiConfig apiConfig;

    public Result getToken(String tenantId,String tenantSecret){
        Auth auth=new Auth();
//        String token= HttpClientUtil.get_token("http://47.92.164.101:8080/api/auth/login",tenantId,tenantSecret)[0];
//        String refreshToken=HttpClientUtil.get_token("http://47.92.164.101:8080/api/auth/login",tenantId,tenantSecret)[1];
        String url=apiConfig.getTokenApi();
        String[] resultStr=HttpClientUtil.get_token_json(url,tenantId,tenantSecret);
        String msg="success";
        if(Integer.valueOf(resultStr[0])!=200){//没有正确拿到信息
            log.error("code:"+resultStr[0]+" result:"+resultStr[1]);
            try{//错误但是返回json格式
                //尝试拿数据
                if(resultStr[1]==null){
                    return new Result(FAIL.code, false,null,"result[1] is null!");
                }
                JSONObject jsonResult = JSONObject.parseObject(resultStr[1]);
                // 错误信息不为null但是没有message项
                if(!jsonResult.containsKey("message")){
                    return new Result(FAIL.code, false,null,resultStr[1]);
                }
                // 有message项
                return new Result(FAIL.code, false, null,jsonResult.get("message").toString());
            }
            catch (JSONException e){//错误而且返回的不是JSON格式
//                System.out.println("resultStr: "+resultStr);
                return new Result(FAIL.code,false,null,resultStr[1]);//存疑，直接返回错误结果还是设null完事
            }
        }
        else{
            //尝试拿数据
            JSONObject jsonResult = JSONObject.parseObject(resultStr[1]);
            log.info(jsonResult.toJSONString());
            String token=jsonResult.get("token").toString();
            String refreshToken=jsonResult.get("refreshToken").toString();
            //Token生成失败的处理
            if(token==null||token.isEmpty()){
                log.error("Token生成失败");
                return new Result(TOKEN_GENERATED_ERROR.code,false,null, "token生成失败");
            }
            auth.setScope("default");
            auth.setTokenType("bearer");
            auth.setExpiresIn(0);//-------------------------------待修改
            auth.setAccessToken(token);
            auth.setRefreshToken(refreshToken);
        }
        return new Result(SUCCESS.code,true,auth, "success");
    }
    public Result getTokenRefresh(String refreshToken){
        Auth auth=new Auth();
        String url=apiConfig.refresTokenApi();
        String[] resultStr=HttpClientUtil.get_token_refresh_json(url,refreshToken);
        String msg="success";
        if(Integer.valueOf(resultStr[0])!=200){//没有正确拿到信息
            log.error("code:"+resultStr[0]+" result:"+resultStr[1]);
            try{//错误但是返回json格式
                //尝试拿数据
                if(resultStr[1]==null){
                    return new Result(FAIL.code, false,null,"result[1] is null!");
                }
                JSONObject jsonResult = JSONObject.parseObject(resultStr[1]);
                // 错误信息不为null但是没有message项
                if(!jsonResult.containsKey("message")){
                    return new Result(FAIL.code, false,null,resultStr[1]);
                }
                // 有message项
                return new Result(FAIL.code, false, null,jsonResult.get("message").toString());
            }
            catch (JSONException e){//错误而且返回的不是JSON格式
//                System.out.println("resultStr: "+resultStr);
                return new Result(FAIL.code,false,null,resultStr[1]);//存疑，直接返回错误结果还是设null完事
            }
        }
        else{
            //尝试拿数据
            JSONObject jsonResult = JSONObject.parseObject(resultStr[1]);
            log.info(jsonResult.toJSONString());
            String token=jsonResult.get("token").toString();
            String refreshToken_new=jsonResult.get("refreshToken").toString();
            //Token生成失败的处理
            if(token==null||token.isEmpty()){
                log.error("Token生成失败");
                return new Result(TOKEN_GENERATED_ERROR.code,false,null, "token生成失败");
            }
            auth.setScope("default");
            auth.setTokenType("bearer");
            auth.setExpiresIn(0);//-------------------------------待修改
            auth.setAccessToken(token);
            auth.setRefreshToken(refreshToken_new);
        }
        return new Result(SUCCESS.code,true,auth, "success");

//        return null;
    }
}
